package com.heyqing;

import java.util.Scanner;

/**
 * ClassName:BinomialCoefficient
 * Package:com.heyqing
 * Description:
 *
 * @Date:2024/6/21
 * @Author:Heyqing
 */
public class BinomialCoefficient {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入二项式的幂次：");
        int n = sc.nextInt();
        sc.close();
        int[][] pascalTriangle = new int[n+1][];
        computePascalTriangle(pascalTriangle);
        printBinomialCoefficients(pascalTriangle, n);
    }
    private static void computePascalTriangle(int[][] triangle) {
        for (int i = 0; i < triangle.length; i++) {
            triangle[i] = new int[i+1];
            triangle[i][0] = 1; // 每行的第一个元素是1
            triangle[i][i] = 1; // 每行的最后一个元素是1
            for (int j = 1; j < i; j++) {
                triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; // 中间的元素是上一行相邻两个元素之和
            }
        }
    }
    private static void printBinomialCoefficients(int[][] triangle, int n) {
        System.out.printf("二项式 (a+b)^%d 的系数为：%n", n);
        for (int i = 0; i <= n; i++) {
            System.out.printf("%d ", triangle[n][i]);
        }
        System.out.println();
    }
}